package com.common;

import java.util.HashMap;

import javax.servlet.http.HttpSession;

/*
 * 管理整个程序的session
*/
public class SessionContext {
	private static SessionContext instance;
    private HashMap<Object, Object> mymap;
    //是否重复登录
    private Boolean repeatLogin =true;

    private SessionContext() {
        mymap = new HashMap<Object, Object>();
    }

    public static SessionContext getInstance() {
        if (instance == null) {
            instance = new SessionContext();
        }
        return instance;
    }

    public synchronized void AddSession(Object Id,HttpSession session) {
        if (session != null) {
            mymap.put(Id, session);
        }
    }

	public synchronized void DelSession(Object Id) {
        if (Id != null) {
        	 HttpSession sessionEntity =getSession(Id);
			 if (sessionEntity != null) {
				 sessionEntity.invalidate();
				 mymap.remove(Id);
			 }
        }
    }

    public synchronized HttpSession getSession(Object Id) {
        if (Id == null)
            return null;
        return (HttpSession) mymap.get(Id);
    }
    
    public synchronized HashMap<Object, Object> getAllSession() {
        return mymap;
    }

	public void setRepeatLogin(Boolean repeatLogin) {
		this.repeatLogin = repeatLogin;
	}

	public Boolean getRepeatLogin() {
		return repeatLogin;
	}
}
